<template>
  <div class="messageComponentstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small">
      <h5 style="color: #000; font-size: 14px">添加内容</h5>
      <p style="color: #969799; font-size: 12px; margin-top: 10px">
        拖动选中的导航可对其排序
      </p>
      <!-- 类型列表 -->
      <div>
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
              <i class="el-icon-circle-close" @click="deleteimg(index)" />
              <!-- 图片 -->
              <div class="imag">
                <img
                  src="../../../assets/images/add.png"
                  @click="showUpload('second', index)"
                  alt
                  draggable="false"
                  v-if="!item.src"
                />
                <img
                  :src="item.src"
                  @click="showUpload('second', index)"
                  alt
                  draggable="false"
                  v-else
                />
              </div>
              <!-- 标题和链接 -->
              <div class="imgText">
                <el-input v-model="item.text" placeholder="请输入消息类型标题" size="mini" clearable />
                <div style="height: 5px" />
                <el-input v-model="item.desc" placeholder="请输入消息的描述信息" size="mini" clearable />
                <div style="height: 5px" />
                <!-- 选择类型 -->
                <select-link
                  @selectThisLink="selectThisLink"
                  @clearThisLink="clearThisLink(index)"
                  :linktypeurl="item.url"
                  :theIndex="index"
                />
              </div>
            </section>
          </transition-group>
        </vuedraggable>
      </div>

      <el-button class="uploadImg" type="primary" plain @click="addMessage">
        <i class="el-icon-plus" />点击添加内容
      </el-button>
    </el-form>

    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import uploadimg from "../../uploadImg"; // 图片上传
import SelectLink from "../../selectLink";
export default {
  name: "MessageComponentstyle",
  components: {
    vuedraggable,
    uploadimg,
    SelectLink,
  },
  props: {
    datas: Object,
  },
  data() {
    return {
      dragOptions: {
        animation: 200,
      },
      changeType: "",
      changeIndex: "",
    };
  },
  methods: {
    inputFocus(index) {
      this.datas.imageList[index].text = "";
    },
    showUpload(type, index) {
      this.changeType = type;
      this.changeIndex = index;
      this.$refs.upload.showUpload();
    },
    addMessage() {
      this.datas.imageList.push({
        src: "",
        text: "这里是消息类型标题",
        desc: "这里是消息类型的描述信息",
      });
    },
    // 提交
    uploadInformation(res) {
      if (this.changeType == "first") {
        this.datas.imageList.push({
          src: res,
          text: "这里是消息类型标题",
          desc: "这里是消息类型的描述信息",
        });
      } else {
        this.datas.imageList[this.changeIndex].src = res;
      }
    },

    // 链接选择
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
        }
      });
    },

    clearThisLink(index) {
      this.datas.imageList[index].url = "";
    },

    /* 删除图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.messageComponentstyle {
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  .lef {
    .el-form-item__label {
      text-align: left;
    }
    p {
      line-height: 4px;
    }
  }
  /* 图片广告列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imag {
      width: 60px;
      height: 60px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }

    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 20px;
      justify-content: space-between;
      .select-type {
        display: flex;
        .el-select {
          .el-input {
            input {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
          }
        }
      }
    }
  }
}
</style>
